package com.briup.app02.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.app02.bean.Address;
import com.briup.app02.bean.AddressExample;
import com.briup.app02.dao.AddressMapper;
import com.briup.app02.dao.extend.AddressExtendMapper;
import com.briup.app02.service.IAddressService;
import com.briup.app02.utils.CustomerException;



@Service
public class AddressServiceImpl implements IAddressService{
	@Autowired
	private AddressMapper addressMapper;
	@Autowired
	private AddressExtendMapper addressExtendMapper;

	@Override
	public void saveOrUpdate(Address address) throws CustomerException{
		if(address.getId()!=null) {
			addressMapper.updateByPrimaryKey(address);
		}else {
			AddressExample example=new AddressExample();
			example.createCriteria().andAddressEqualTo(address.getUsername());
			List<Address>list=addressMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的分类已存在");
			}
			addressMapper.insert(address);
		}
	}
	
	@Override
	public List<Address> query(String name){
		AddressExample example=new AddressExample();
		if(name!=null) {
			example.createCriteria().andUsernameLike("%"+name+"%");
		}
		List<Address>list=addressMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public void deleteById(long id)throws CustomerException{
		Address address=addressMapper.selectByPrimaryKey(id);
		if(address==null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		addressMapper.deleteByPrimaryKey(id);
	}
	
	@Override
	public List<Address>pageQuery(int page, int pageSize, Long userId){
		int pageIndex=(page-1)*pageSize;
		List<Address>address=addressExtendMapper.pageQuery(pageIndex, pageSize, userId);
		return address;
	}
}		
	/*@Override
	public List<Address> findAll(){
		AddressExample example=new AddressExample();
		return addressMapper.selectByExample(example);
	}
	
	@Override
	public void saveOrUpdate(Address address) {
		if(address.getId()==null) {
			addressMapper.insert(address);
		}else {
			addressMapper.updateByPrimaryKey(address);
		}
	}
	
	@Override
	public Address findById(long id) {
		return addressMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public  void deleteById(long id) {
		addressMapper.deleteByPrimaryKey(id);
		
	}
	*/

